package com.wu.foodsafetyplayer.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dueeeke.videoplayer.ijk.IjkPlayerFactory;
import com.dueeeke.videoplayer.player.VideoView;
import com.wu.foodsafetyplayer.R;

/**
 * GridView适配器
 *
 * @author zihao
 */
public class MainVideoViewAdapter extends BaseAdapter {
    private static final String TAG = MainVideoViewAdapter.class.getSimpleName();
    private LayoutInflater inflater;
    private String[] itemNames = new String[]{"摄像头1", "摄像头2", "摄像头3", "摄像头4"};
    private String[] itemUrls = new String[]{"rtsp://10.9.0.102:554/user=admin&password=&channel=0&stream=0.sdp?", "rtsp://10.9.0.155:554/user=admin&password=&channel=0&stream=1.sdp?", "rtsp://10.9.0.106:554/user=admin&password=abc-123&channel=0&stream=0.sdp?", "rtsp://10.9.0.154:554/user=admin&password=&channel=0&stream=0.sdp?"};
    private Context context;
    private int widht, height, playerNum;
    private String playerUrl;

    public MainVideoViewAdapter(Context context, int widht, int height, int playerNum, String url) {
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.widht = widht;
        this.height = height;
        this.playerNum = playerNum;
        this.playerUrl = url;
    }

    @Override
    public int getCount() {
        if (TextUtils.isEmpty(playerUrl)) {
            return itemNames.length;
        } else {
            return 1;
        }
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        if (TextUtils.isEmpty(playerUrl)) {
            return itemNames[position];
        } else {
            return playerUrl;
        }
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_video_view, null);
            holder = new ViewHolder();
            holder.itemName = convertView.findViewById(R.id.item_videoview_name);
            holder.videoView = convertView.findViewById(R.id.item_videoview);
            holder.videoView.setFocusable(false);
            holder.videoView.setFocusableInTouchMode(false);
            holder.videoView.setScreenScaleType(1);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Log.e(TAG, "playerNum: " + playerNum);
        //2分屏
        if (playerNum == 2) {
            holder.videoView.getLayoutParams().width = widht / 2;
            holder.videoView.getLayoutParams().height = height;
        }
        //4分屏
        else if (playerNum == 4) {
            holder.videoView.getLayoutParams().width = widht / 2;
            holder.videoView.getLayoutParams().height = height / 2;
        }
        //默认一个
        else {
            holder.videoView.getLayoutParams().width = widht;
            holder.videoView.getLayoutParams().height = height - 5;
        }

        holder.itemName.setText(itemNames[position]);

        holder.videoView.setPlayerFactory(IjkPlayerFactory.create());
        if (TextUtils.isEmpty(playerUrl)) {
            holder.videoView.release();
            holder.videoView.setUrl(itemUrls[position]);
            holder.videoView.start();
        } else {
            holder.videoView.release();
            holder.videoView.setUrl(playerUrl);
            holder.videoView.start();
        }
        return convertView;
    }

    /**
     * 工具类
     */
    private class ViewHolder {
        TextView itemName;
        VideoView videoView;
    }

}
